package com.oig.sys.workflow.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.workflow.dao.ITbAutoOperationDao;
import com.oig.sys.workflow.dto.AutoOperationDto;
import com.oig.sys.workflow.entity.TbAutoOperation;
import com.oig.sys.workflow.entity.TbAutoRule;
import com.oig.sys.workflow.service.ITbAutoOperationService;
import com.oig.sys.workflow.vo.AutoOperationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Slf4j
@Service
public class TbAutoOperationServiceImpl implements ITbAutoOperationService {

	private final ITbAutoOperationDao tbAutoOperationDao;

	@Autowired
	public TbAutoOperationServiceImpl(ITbAutoOperationDao tbAutoOperationDao){
		 this.tbAutoOperationDao = tbAutoOperationDao;
	}



	private AutoOperationVo addAutoOperation(AutoOperationDto autoOperationDto){
		TbAutoOperation autoOperation = new TbAutoOperation();
		BeanUtils.copyProperties(autoOperationDto, autoOperation,"id");
		this.tbAutoOperationDao.save(autoOperation) ;
		return this.buildVo(autoOperation);
	}


	private AutoOperationVo updateAutoOperation(AutoOperationDto autoOperationDto){
		TbAutoOperation autoOperation = this.tbAutoOperationDao.getAutoOpById(autoOperationDto.getId()) ;
		if (autoOperation==null){
			throw new BadRequestException("条件分支操作不存在") ;
		}
		if (!autoOperation.getAutoRuleId().equals(autoOperationDto.getAutoRuleId())){
			throw new BadRequestException("条件分支id传错") ;
		}
		BeanUtils.copyProperties(autoOperationDto, autoOperation,"id");
		this.tbAutoOperationDao.save(autoOperation) ;
		return this.buildVo(autoOperation);
	}

	@Transactional
	@Override
	public List<AutoOperationVo> addAutoOperationList(TbAutoRule autoRule, List<AutoOperationDto> autoOperationDtoList) {
		List<AutoOperationVo> operationVoList = new ArrayList<>();
		for (AutoOperationDto autoOperationDto: autoOperationDtoList){
			autoOperationDto.setAutoRuleId(autoRule.getId());
			operationVoList.add(this.addAutoOperation(autoOperationDto));
		}
		return operationVoList;
	}


	@Transactional
	@Override
	public List<AutoOperationVo> updateAutoOperationList(TbAutoRule autoRule, List<AutoOperationDto> autoOperationDtoList) {
		List<AutoOperationVo> operationVoList = new ArrayList<>();
		List<Integer> idList = new ArrayList<>();
		for (AutoOperationDto autoOperationDto: autoOperationDtoList){
			autoOperationDto.setAutoRuleId(autoRule.getId());
			AutoOperationVo autoOperationVo ;
			if (autoOperationDto.getId()==null){
				autoOperationVo = this.addAutoOperation(autoOperationDto);
			} else {
				autoOperationVo = this.updateAutoOperation(autoOperationDto) ;
			}
			idList.add(autoOperationVo.getId());
			operationVoList.add(autoOperationVo);
		}
		//如果有索引不用这么麻烦
		List<Integer> delIdList = this.tbAutoOperationDao.findAllDelId(autoRule.getId(), idList);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbAutoOperationDao.deleteByIdList(delIdList) ;
		}
		return operationVoList;
	}


	private AutoOperationVo buildVo(TbAutoOperation autoOperation){
		AutoOperationVo autoOperationVo = new AutoOperationVo();
		BeanUtils.copyProperties(autoOperation, autoOperationVo);
		return autoOperationVo;
	}

	@Override
	public List<AutoOperationVo> findByRuleId(Integer ruleId) {
		List<AutoOperationVo> autoOperationVoList = new ArrayList<>();
		List<TbAutoOperation> autoOperationList =  this.tbAutoOperationDao.findByAutoRuleId(ruleId) ;
		if (CollectionUtil.isNotEmpty(autoOperationList)){
			autoOperationList.forEach((autoOperation)-> autoOperationVoList.add(this.buildVo(autoOperation)) );
		}
		return autoOperationVoList;
	}


	@Transactional
	@Override
	public void deleteByRuleId(Integer ruleId) {
		List<Integer> delIdList = this.tbAutoOperationDao.findAllIdByRuleId(ruleId);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbAutoOperationDao.deleteByIdList(delIdList) ;
		}
	}

}
